package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_CROSS_TRADE_WAR_JOIN_UNION_LIST, desc = "玩家查询当前活动当前分组参与的商会")
public class CrossTradeWarGetCanBattleUnion extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CanCrossTradeWarUnionNameReq msg = CrossTradeWarProto.CanCrossTradeWarUnionNameReq.parseFrom(packet.getBytes());
        activityId = msg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo openActivityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            ret = GameErrorCode.E_UNION_ACTIVITY_GROUP_NO_ABLE;
            return;
        }
        ret = player.getModule(TradeWarModule.class).getTradeWarActivityJoinUnion(activityId, groupId);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            CrossTradeWarProto.CanCrossTradeWarUnionNameResp.Builder builder = CrossTradeWarProto.CanCrossTradeWarUnionNameResp.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_CROSS_TRADE_WAR_JOIN_UNION_LIST, builder);
        }
    }
}
